package com.zhangqi.qiojbackendjudgeservice.init;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;



/**
 * 用于创建测试程序用到的交换机和队列（只用在程序启动前执行一次）
 *
 * @author zhangqi
 */
@Slf4j
public class CodeMqInitMain {

    private static final String CODE_EXCHANGE_NAME = "code_exchange";
    private static final String CODE_DLX_QUEUE = "abc";
    private static final String CODE_DIRECT_EXCHANGE = "direct";
    private static final String CODE_QUEUE = "code_queue";

    public static void doInitCodeMq() {
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("47.108.209.10");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            String codeExchangeName = CODE_EXCHANGE_NAME;
            channel.exchangeDeclare(codeExchangeName, CODE_DIRECT_EXCHANGE);

            // 创建 code 队列
            String codeQueue = CODE_QUEUE;
            channel.queueDeclare(codeQueue, true, false, false, null);
            channel.queueBind(codeQueue, codeExchangeName, "my_routingkey");
            log.info("消息队列启动成功！");
        } catch (Exception e) {
            log.error("消息队列启动失败");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        doInitCodeMq();
    }
}